package jam.network.message;

import jam.model.*;
import jam.network.*;
import java.util.*;

//
// =============================================================================
//

public class ClientActionLock extends ClientCommand {
    protected static final String KEYWORD = "CA_LOCK";
    private String channelName;
    private boolean locked;
    
    //
    // =========================================================================
    //
    
    /**
     */
    @Override
    public void process(ClientController controller) {
        NetworkController network = controller.getNetworkController();
        ClientModel client = controller.getModel();
        ServerModel server = network.getModel();
        
        // Try and get the channel. Fail if we aren't in that channel.
        Channel channel = client.getChannel(this.channelName);
        if (channel == null) {
            controller.send(new ServerResponse(this, false));
            return;
        }
 
        // Lock the channel.
        channel.setLocked(locked);
        
        // Respond to the first client.
        controller.send(new ServerResponse(this, true));
    }
    
    //
    // =========================================================================
    //
    
    @Override
    protected String getKeyword() { return ClientActionLock.KEYWORD; }
    
    //
    // =========================================================================
    //
    
    /**
     * Constructor. Processes the data string.
     *
     * @param transactionId The identification number for this particular message.
     * @param args A scanner with the arguments
     */
    protected ClientActionLock (int transactionId, Scanner args) {
        super(transactionId);
        
        // Read in the message arguments.
        this.channelName = args.next();
        this.locked = args.nextBoolean();
    }
}